import 'package:flutter/material.dart';
// import 'package:hight_app/demo/bloc/bloc_demo.dart';
import 'package:hight_app/demo/drawer_demo.dart';
// import 'package:hight_app/demo/material_components.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'code_book.dart';
import 'memo_list.dart';
import 'mood_list.dart';
import 'task_list.dart';

class BottomNavigationBarDemo extends StatefulWidget {
  @override
    State<StatefulWidget> createState() {
      // TODO: implement createState
      return _BottomNavigationBarDemoState();
    }
}

class _BottomNavigationBarDemoState extends State<BottomNavigationBarDemo> {
  // with SingleTickerProviderStateMixin {
    final _bottomNavigationColor = Colors.grey;
    final _bottomNavigationSelectColor = Colors.blue;
    int _currentIndex = 0;
    var _controller = PageController(
      initialPage: 0,
    );
    void initState(){
      super.initState();
      _validateLogin();
    }
    @override
    void dispose() {
      super.dispose();
      _controller.dispose();
    }
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('NINGHAO'),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              tooltip: 'Search',
              onPressed: () => debugPrint('Search button is pressed.'),
            )
          ],
          elevation: 0.0
        ),
        drawer: DrawerDemo(),
        body: PageView(
          controller: _controller,
          children: <Widget>[
            CodeBook(),
            TaskList(),
            MemoList(), // MaterialComponents(),
            MoodList(),// BlocDemo(),
          ],
          physics: NeverScrollableScrollPhysics(),
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            _controller.jumpToPage(index);
            setState(() {
              _currentIndex = index;
            });
          },
          // onTap: _onTapHandler,
          type: BottomNavigationBarType.fixed,
          fixedColor: Colors.black,
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.explore),
              title: Text('密码本'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.history),
              title: Text('任务清单'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.list),
              title: Text('便签随笔'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text('心情语录'),
            ),
          ],
        ),
      );
    }

  Future _validateLogin() async{
    Future<dynamic> future = Future(()async{
      SharedPreferences prefs =await SharedPreferences.getInstance();
      return prefs.getString("user_token");
    });
    future.then((val){
      if(val == null) {
        Navigator.of(context).pushNamedAndRemoveUntil('/login', (route) => route == null,);
      }
    }).catchError((_){
        print("catchError");
    });
  }
}
